package com.xposed.xinhao.hook_weixin;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;

import com.xposed.xinhao.utils.LogUtils;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage;

public class WechatHook {

    private static final String TAG = "MainXposed";

    //保存上下文
    private Context mContext;

    private XC_LoadPackage.LoadPackageParam lpparam;

    public WechatHook(XC_LoadPackage.LoadPackageParam lpparam) {
        this.lpparam = lpparam;
    }


    public void hookMethod() {

        XposedHelpers.findAndHookMethod(Activity.class, "onCreate", Bundle.class, new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                mContext = (Context) param.thisObject;
                LogUtils.e("Activity onCreate：" + mContext.getClass().getName());
            }
        });
    }

    /**
     * 主动调起微信生成收款码方法
     */
    private void callMethodGetQRCode(XC_LoadPackage.LoadPackageParam lpparam, double money, String desc) {
        Class<?> aClass = XposedHelpers.findClassIfExists("com.tencent.mm.plugin.collect.model.s", lpparam.classLoader);
        Object aObject = XposedHelpers.newInstance(aClass, new Class[]{double.class, String.class, String.class}, money, "1", desc);
        Class<?> bClass = XposedHelpers.findClassIfExists("com.tencent.mm.wallet_core.d.f", lpparam.classLoader);
        Object bObject = Proxy.newProxyInstance(lpparam.classLoader, new Class[]{bClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                return null;
            }
        });
        Class<?> cClass = XposedHelpers.findClassIfExists("com.tencent.mm.wallet_core.d.i", lpparam.classLoader);
        Object cObject = XposedHelpers.newInstance(cClass, new Class[]{Context.class, bClass}, mContext, bObject);
        XposedHelpers.callMethod(cObject, "a", aObject, true, 1, 0);
    }
}
